package cases.chapter12.chapter1203;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class ReflexTest {
    public static void main(String[] args) throws IOException,
            ClassNotFoundException, InstantiationException, IllegalAccessException,
            NoSuchMethodException, SecurityException, IllegalArgumentException,
            InvocationTargetException {
        /*1、加载配置文件
         * 用类名.class.getResourceAsStream("/xx")或者
         * 类名.class.getClassLoader().getResourceAsStream("xx");
         * 区别在于前者是需要反斜杠，后者不需要
         * */
        Properties properties = new Properties();
        properties.load(ReflexTest.class.getResourceAsStream("" +
                "/test.properties"));
        //2、获取配置文件中定义的数据
        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");
        //3、加载该类进内存
        Class cls = Class.forName(className);
        //4、创建类对象
        Object obj = cls.newInstance();
        //5、获取对象方法
        Method method = cls.getMethod(methodName);
        //6、执行方法
        method.invoke(obj);
    }
}
